﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class Nutrition : System.Web.UI.Page
    {
        private mamnontuoitho7.AppCode.Article _article = new mamnontuoitho7.AppCode.Article();
        private ArticleType _articleType = new ArticleType();
        protected Product _product = new Product();
        protected General _general = new General();
        protected Comment _comment = new Comment();

        protected DataTable _dt;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalArticleNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected String _currentSubArticleID = "3";

        protected void Page_Load(object sender, EventArgs e)
        {

            Session["Common_articleTypeName"] = "";
            if (Request.QueryString["SubArticle"] != null)
            {
                string temp2 = Request.QueryString["SubArticle"].ToString();
                Match match2 = Regex.Match(temp2, @"([0-9])$", RegexOptions.IgnoreCase);

                if (temp2.Length <= 4 && match2.Success)
                {
                    bool check = false;

                    try
                    {
                        check = _articleType.checkArticleSuperior(temp2, Session["Common_articleTypeID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorLoginPage);
                    }

                    if (check)
                    {
                        try
                        {
                            _currentSubArticleID = temp2;
                            Session["Common_articleTypeName"] = _articleType.GetOneArticleTypeByID(_currentSubArticleID)["ArticleTypeName"].ToString();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Response.Redirect(ConstantClass._errorLoginPage);
                        }
                    }

                    else { Response.Redirect("Home.aspx"); }
                }
                else
                {
                    Response.Redirect("Home.aspx");
                }

            }


            try
            {
                ///////////////////////////// Start paging
                _totalArticleNumber = _article.TotalArticleOfOneType(_currentSubArticleID).GetValueOrDefault();
                _totalPages = _general.Divide(_totalArticleNumber, ConstantClass._pageStepArticle);
                _totalBars = _general.Divide(_totalPages, ConstantClass._barStepArticle);

                if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                {
                    string temp = Request.QueryString["page"].ToString();
                    Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                    if (temp.Length <= 4 && match.Success)
                    {
                        _pageflag = Convert.ToInt16(temp);// trang hiện hành
                        if (_pageflag <= _totalPages)
                        {
                            _currentbar = _general.Divide(_pageflag, ConstantClass._barStepArticle);
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        else { 
                            _pageflag = 1; _currentbar = 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                    }
                }
                if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                {
                    _currentbar = 1;
                    _pageflag = 1;
                    Response.Cookies["CurrentBar"].Value = "1";
                }

                if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                {
                    _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                    if (_currentbar < _totalBars)
                    {
                        _currentbar += 1;
                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                    }
                    _pageflag = (_currentbar - 1) * ConstantClass._barStepArticle + 1;

                }

                if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                {
                    _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                    if (_currentbar > 1)
                    {
                        _currentbar -= 1;
                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                    }
                    _pageflag = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                }

                _dt = _article.GetArticlePaged(_pageflag, ConstantClass._pageStepArticle, _currentSubArticleID);
                _beginPage = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                _endPage = _currentbar * ConstantClass._barStepArticle;

                if (_endPage > _totalPages)
                {
                    _endPage = _totalPages;
                }
                ///////////////////////////// End paging

                Session["Common_articleTypeID"] = "3";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }
    }
}